// 笔试强训day36:哈夫曼编码

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int main() {
    int n, tmp; cin >> n;
    priority_queue<long long, vector<long long>, greater<long long>> heapq;
    while (n--)
    {
        cin >> tmp;
        heapq.emplace(tmp);
    }
    long long ans = 0;
    while (heapq.size() >= 2)
    {
        long long num1 = heapq.top(); heapq.pop();
        long long num2 = heapq.top(); heapq.pop();

        long long newnum = num1 + num2;
        heapq.emplace(newnum);
        ans += newnum;
    }
    cout << ans << endl;
}